using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace MicroMsg.UI.Page
{
	public class BrandListPage : BasePage
	{
		private const string TAG = "BrandListPage";
		private bool mBIsFirstLoaded = true;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(Contact), typeof(BrandListPage), new PropertyMetadata(null, new PropertyChangedCallback(BrandListPage.OnItemObjectPropertyChanged)));
		private EventWatcher mWatchContact;
		private object mObjectWatcher = new object();
		private string mStrCurAccBrandList = "";
		private bool mBIsUser;
		private bool mBIsShowing;
		private bool mBIsFirstShowing = true;
		private Dictionary<string, bool> mDictBrandMap = new Dictionary<string, bool>();
		internal Grid LayoutRoot;
		internal ScrollViewer SvRoot;
		internal TextBlock txtTitle;
		internal TextBlock txtShow;
		internal Paragraph paraContent;
		internal StackPanel panelNotShow;
		internal TextBlock txtNotShow;
		internal Paragraph paraNotShowContent;
		private bool _contentLoaded;
		public Contact ItemObject
		{
			get
			{
				return (Contact)base.GetValue(BrandListPage.ItemObjectProperty);
			}
			set
			{
				base.SetValue(BrandListPage.ItemObjectProperty, value);
			}
		}
		public BrandListPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Page_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Page_Unloaded));
		}
		private void Page_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("BrandListPage", "Page_Loaded");
			if (this.mBIsFirstLoaded)
			{
				this.mBIsFirstLoaded = false;
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.RefreshItem();
				});
			}
		}
		private void Page_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("BrandListPage", "Page_Unloaded");
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("BrandListPage", "OnPageIn");
			this.registerEvent();
			if (object_0 != null && object_0.Length != 0)
			{
				if (object_0[0].GetType() == typeof(Contact))
				{
					this.ItemObject = (object_0[0] as Contact);
					return;
				}
			}
			base.GoBack();
		}
		protected override void OnPageOut()
		{
			Log.smethod_5("BrandListPage", "OnPageOut");
			this.unRegisterEvent();
		}
		protected override void OnPageBack()
		{
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == BrandListPage.ItemObjectProperty)
			{
				(dependencyObject_0 as BrandListPage).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void registerEvent()
		{
			if (this.mWatchContact == null)
			{
				this.mWatchContact = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnContactEvent));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_ACCOUNT_UPDATED, this.mWatchContact);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchContact != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_ACCOUNT_UPDATED, this.mWatchContact);
				this.mWatchContact = null;
			}
		}
		private void OnContactEvent(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			EventConst mEventID = evtArgs.mEventID;
			if (mEventID != EventConst.ON_STORAGE_ACCOUNT_UPDATED)
			{
				return;
			}
			if (this.mBIsUser)
			{
				Account curAccount = AccountMgr.getCurAccount();
				if (curAccount != null && curAccount.MyBrandList != this.mStrCurAccBrandList)
				{
					this.ItemObject.strMyBrandList = curAccount.MyBrandList;
					this.RefreshItem();
				}
			}
		}
		private void RefreshItem()
		{
			if (this.mBIsFirstLoaded)
			{
				return;
			}
			if (this.ItemObject == null)
			{
				return;
			}
			if (this.mBIsFirstShowing)
			{
				this.panelNotShow.set_Visibility(1);
				this.txtShow.set_Visibility(1);
			}
			this.mBIsUser = false;
			if (this.ItemObject.strUsrName == AccountMgr.strUsrName)
			{
				this.txtShow.set_Text(strings.BrandStoryDetail_LIKE_SECTION_SELF);
				this.ItemObject = ContactMgr.GetContactFromAccount(false);
				this.mStrCurAccBrandList = this.ItemObject.strMyBrandList;
				this.mBIsUser = true;
			}
			if (this.mBIsShowing)
			{
				return;
			}
			List<BrandUser> listCons = this.ItemObject.getBrandList();
			this.syncFollowedContacts(listCons);
			if (listCons == null)
			{
				return;
			}
			this.mBIsShowing = true;
			if (this.mBIsFirstShowing)
			{
				ProcessingCover.getProgressBar(this.LayoutRoot, strings.COM_Loading, null, false, false, null);
				this.mBIsFirstShowing = false;
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				using (List<BrandUser>.Enumerator enumerator = listCons.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						BrandUser current = enumerator.get_Current();
						if (this.mBIsUser && current.nHidden == 1u)
						{
							if (this.panelNotShow.get_Visibility() == 1)
							{
								this.txtNotShow.set_Text(strings.BrandStoryDetail_LIKE_SECTION_SELF_HIDDEN);
								this.panelNotShow.set_Visibility(0);
							}
							this.updtateConToPara(current, false);
						}
						else
						{
							if (this.txtShow.get_Visibility() == 1 && this.ItemObject.strUsrName == AccountMgr.strUsrName)
							{
								this.txtShow.set_Visibility(0);
							}
							this.updtateConToPara(current, true);
						}
					}
				}
				if (this.paraContent.get_Inlines().get_Count() == 0)
				{
					this.txtShow.set_Visibility(1);
				}
				if (this.paraNotShowContent.get_Inlines().get_Count() == 0)
				{
					this.panelNotShow.set_Visibility(1);
				}
				this.mBIsShowing = false;
				this.get_Dispatcher().BeginInvoke(delegate
				{
					ProcessingCover.releaseProgressBar(this.LayoutRoot);
				});
			});
		}
		private void updtateConToPara(BrandUser brandUser_0, bool bIsShow)
		{
			if (brandUser_0 != null && !string.IsNullOrEmpty(brandUser_0.strUsrName))
			{
				Paragraph paragraph;
				if (bIsShow)
				{
					paragraph = this.paraContent;
				}
				else
				{
					paragraph = this.paraNotShowContent;
				}
				if (this.mDictBrandMap.ContainsKey(brandUser_0.strUsrName))
				{
					if (this.mDictBrandMap.get_Item(brandUser_0.strUsrName) == bIsShow)
					{
						return;
					}
					Inline inline = this.removeConFromPara(brandUser_0.strUsrName);
					if (inline != null)
					{
						paragraph.get_Inlines().Add(inline);
						this.mDictBrandMap.set_Item(brandUser_0.strUsrName, bIsShow);
						return;
					}
				}
				this.mDictBrandMap.set_Item(brandUser_0.strUsrName, bIsShow);
				InlineUIContainer inlineUIContainer = new InlineUIContainer();
				BrandHeadCtrl brandHeadCtrl = new BrandHeadCtrl();
				brandHeadCtrl.set_Height(75.0);
				brandHeadCtrl.set_Width(75.0);
				brandHeadCtrl.set_Margin(new Thickness(10.0, 23.0, 27.0, 23.0));
				brandHeadCtrl.ItemObject = brandUser_0.ToContact();
				inlineUIContainer.set_Child(brandHeadCtrl);
				paragraph.get_Inlines().Add(inlineUIContainer);
				return;
			}
		}
		private InlineUIContainer removeConFromPara(string strName)
		{
			if (string.IsNullOrEmpty(strName))
			{
				return null;
			}
			if (this.mDictBrandMap.ContainsKey(strName))
			{
				bool flag = this.mDictBrandMap.get_Item(strName);
				Paragraph paragraph;
				if (flag)
				{
					paragraph = this.paraContent;
				}
				else
				{
					paragraph = this.paraNotShowContent;
				}
				InlineUIContainer result;
				using (IEnumerator<Inline> enumerator = paragraph.get_Inlines().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Inline current = enumerator.get_Current();
						if (current != null)
						{
							InlineUIContainer inlineUIContainer = current as InlineUIContainer;
							if (inlineUIContainer != null && inlineUIContainer.get_Child() != null)
							{
								BrandHeadCtrl brandHeadCtrl = inlineUIContainer.get_Child() as BrandHeadCtrl;
								if (brandHeadCtrl != null && brandHeadCtrl.ItemObject != null && brandHeadCtrl.ItemObject.strUsrName == strName)
								{
									paragraph.get_Inlines().Remove(inlineUIContainer);
									result = inlineUIContainer;
									return result;
								}
							}
						}
					}
					goto IL_C2;
				}
				return result;
			}
			IL_C2:
			return null;
		}
		private void syncFollowedContacts(List<BrandUser> listUsers)
		{
			if (listUsers != null && listUsers.get_Count() != 0)
			{
				List<string> list = new List<string>();
				using (List<BrandUser>.Enumerator enumerator = listUsers.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						BrandUser current = enumerator.get_Current();
						if (!string.IsNullOrEmpty(current.strUsrName))
						{
							list.Add(current.strUsrName);
						}
					}
				}
				using (Dictionary<string, bool>.KeyCollection.Enumerator enumerator2 = this.mDictBrandMap.get_Keys().GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						string current2 = enumerator2.get_Current();
						if (!list.Contains(current2))
						{
							this.removeConFromPara(current2);
						}
					}
				}
				return;
			}
			this.paraContent.get_Inlines().Clear();
			this.paraNotShowContent.get_Inlines().Clear();
		}
		private void releaseCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		private void getCover(string strToShow = null)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strToShow, null, false, false, null);
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Contact/ProfileInfo/BrandListPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.SvRoot = (ScrollViewer)base.FindName("SvRoot");
			this.txtTitle = (TextBlock)base.FindName("txtTitle");
			this.txtShow = (TextBlock)base.FindName("txtShow");
			this.paraContent = (Paragraph)base.FindName("paraContent");
			this.panelNotShow = (StackPanel)base.FindName("panelNotShow");
			this.txtNotShow = (TextBlock)base.FindName("txtNotShow");
			this.paraNotShowContent = (Paragraph)base.FindName("paraNotShowContent");
		}
	}
}
